<?php

namespace app\admin\controller;

use app\BaseController;
use app\model\RatingScaleModel;
use app\model\RatingScaleTypeModel;
use think\App;

class RatingScaleType extends BaseController
{
    protected $mode;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->mode = new RatingScaleTypeModel();
    }

    /**
     * 量表类型列表
     * @return false|string
     */
    public function list(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['name'])) {
                $where[] = ['name', 'like', '%'.$param['name'].'%'];
            }
            $where[] = ['unit_id','=',$this->userData['unit_id']];
            $query = $this->mode->where($where);
            $data = $query->order('sort asc')->paginate($param['pageSize'])->toArray();
//            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 新增量表类型
     * @return false|string
     */
    public function add(): bool|string
    {
        try {
            $param = $this->request->param();
            $param['unit_id'] = $this->userData['unit_id'];
            $this->validate($param, $this->mode->addValidate());
            $param['time'] = time();
            $this->mode->insert($param);
            return success([],'添加成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 更新量表类型
     * @return false|string
     */
    public function update(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, $this->mode->updateValidate());
            $info = $this->mode->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            return success([],'更新成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line'=> $e->getLine(),
                'file'=> $e->getFile()
            ]);
        }
    }

    /**
     * 删除量表类型
     * @return false|string
     */
    public function del(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $info = $this->mode->find($id);
            if (!$info) throw new \Exception('参数错误！');
            $RatingScaleModel = new RatingScaleModel();
            $is = $RatingScaleModel->where('type_id','=',$id)->value('id');
            if ($is) throw new \Exception('分类下存在量表，无法直接删除！');
            $info->is_del = 1;
            $info->del_time = time();
            $info->save();
            return success([],'删除成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line'=> $e->getLine(),
                'file'=> $e->getFile()
            ]);
        }
    }

    /**
     * 量表类型基本信息
     * @return false|string
     */
    public function info(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $info = $this->mode->find($id);
            return success($info);
        }catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }


}
